<script>
	export default {
		onLaunch: function() {
			console.log('App Launch')
			this.initData()
			this.checkLoginStatus()
			this.initInputFix()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			initData() {
				// 初始化默认分类数据
				const defaultCategories = {
					expense: [
						{ name: '餐饮', icon: '🍽️', color: '#FF6B6B' },
						{ name: '交通', icon: '🚗', color: '#4ECDC4' },
						{ name: '购物', icon: '🛒', color: '#45B7D1' },
						{ name: '娱乐', icon: '🎮', color: '#96CEB4' },
						{ name: '医疗', icon: '💊', color: '#FFEAA7' },
						{ name: '教育', icon: '📚', color: '#DDA0DD' },
						{ name: '住房', icon: '🏠', color: '#98D8C8' },
						{ name: '其他', icon: '📝', color: '#F7DC6F' }
					],
					income: [
						{ name: '工资', icon: '💰', color: '#4CAF50' },
						{ name: '奖金', icon: '🎁', color: '#FF9800' },
						{ name: '投资', icon: '📈', color: '#2196F3' },
						{ name: '兼职', icon: '💼', color: '#9C27B0' },
						{ name: '其他', icon: '📝', color: '#607D8B' }
					]
				}
				
				// 保存默认分类
				if (!uni.getStorageSync('accounting_categories')) {
					uni.setStorageSync('accounting_categories', defaultCategories)
				}
				
				// 初始化示例数据（可选）
				if (!uni.getStorageSync('accounting_records')) {
					const sampleRecords = [
						{
							id: '1',
							type: 'expense',
							amount: 25.50,
							category: '餐饮',
							remark: '午餐',
							date: new Date().toISOString().split('T')[0],
							color: '#FF6B6B',
							createTime: new Date().toISOString()
						},
						{
							id: '2',
							type: 'income',
							amount: 5000,
							category: '工资',
							remark: '月薪',
							date: new Date().toISOString().split('T')[0],
							color: '#4CAF50',
							createTime: new Date().toISOString()
						}
					]
					uni.setStorageSync('accounting_records', sampleRecords)
				}
			},
			checkLoginStatus() {
				const currentUser = uni.getStorageSync('currentUser')
				if (!currentUser) {
					// 没有登录，跳转到登录页
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}
			},
			initInputFix() {
				// 初始化输入框修复
				// #ifdef H5
				// 在H5环境下使用DOM API修复输入框
				setTimeout(() => {
					const inputs = document.querySelectorAll('input, textarea')
					inputs.forEach(input => {
						input.style.backgroundColor = '#fff'
						input.style.color = '#333'
						input.style.webkitUserSelect = 'text'
						input.style.userSelect = 'text'
						input.style.webkitAppearance = 'none'
						input.style.appearance = 'none'
						input.style.caretColor = '#667eea'
						input.style.zIndex = '1'
						input.style.position = 'relative'
						input.style.lineHeight = '1.4'
						input.style.verticalAlign = 'middle'
						input.style.height = 'auto'
						input.style.minHeight = '68rpx'
						
						// 如果是textarea，设置不同的垂直对齐
						if (input.tagName === 'TEXTAREA') {
							input.style.verticalAlign = 'top'
							input.style.minHeight = '120rpx'
						}
					})
				}, 100)
				// #endif
			}
		}
	}
</script>

<style>
	/* 全局输入框样式修复 - 更完整的解决方案 */
	
	/* 基础输入框样式 */
	input, textarea {
		background-color: #fff !important;
		color: #333 !important;
		border: none;
		outline: none;
		-webkit-appearance: none;
		appearance: none;
		user-select: text !important;
		-webkit-user-select: text !important;
		-webkit-touch-callout: default;
		-webkit-tap-highlight-color: transparent;
		caret-color: #667eea;
		z-index: 1;
		position: relative;
	}

	/* 确保输入框在uni-app中可以正常输入 */
	.form-input, .item-input, .remark-textarea, .input-field, .test-input, .test-textarea {
		background-color: #fff !important;
		color: #333 !important;
		border: 2rpx solid #f0f0f0;
		border-radius: 12rpx;
		padding: 20rpx;
		font-size: 28rpx;
		line-height: 1.4;
		box-sizing: border-box;
		-webkit-appearance: none;
		appearance: none;
		user-select: text !important;
		-webkit-user-select: text !important;
		z-index: 1;
		position: relative;
		height: auto;
		min-height: 68rpx;
		vertical-align: middle;
	}

	/* 输入框聚焦状态 */
	.form-input:focus, .item-input:focus, .remark-textarea:focus, .input-field:focus, .test-input:focus, .test-textarea:focus {
		border-color: #667eea;
		background-color: #fff !important;
		outline: none;
		-webkit-appearance: none;
		appearance: none;
	}

	/* 修复textarea样式 */
	textarea {
		resize: none;
		overflow: auto;
		background-color: #fff !important;
		color: #333 !important;
	}

	/* 确保按钮可以正常点击 */
	button {
		-webkit-appearance: none;
		appearance: none;
		border: none;
		outline: none;
	}

	/* 修复picker样式 */
	picker {
		background-color: #fff;
	}

	/* 确保输入框在移动端可以正常输入 */
	input[type="text"], 
	input[type="number"], 
	input[type="password"], 
	input[type="email"], 
	input[type="tel"], 
	input[type="digit"],
	textarea {
		-webkit-user-select: text !important;
		user-select: text !important;
		-webkit-touch-callout: default;
		-webkit-tap-highlight-color: transparent;
		background-color: #fff !important;
		color: #333 !important;
		z-index: 1;
		position: relative;
	}

	/* 修复输入框在iOS上的问题 */
	input {
		-webkit-appearance: none;
		appearance: none;
		border-radius: 0;
		background-color: #fff !important;
		color: #333 !important;
	}

	/* 确保输入框内容可见 */
	input, textarea {
		caret-color: #667eea;
		background-color: #fff !important;
		color: #333 !important;
	}

	/* 修复输入框在uni-app中的常见问题 */
	uni-input, uni-textarea {
		background-color: #fff !important;
		color: #333 !important;
	}

	/* 确保输入框在移动端可以正常聚焦 */
	input:focus, textarea:focus {
		outline: none;
		border-color: #667eea;
		background-color: #fff !important;
		color: #333 !important;
		-webkit-appearance: none;
		appearance: none;
	}

	/* 修复iOS Safari中的输入框问题 */
	@supports (-webkit-touch-callout: none) {
		input, textarea {
			font-size: 28rpx; /* 保持与设计一致 */
			background-color: #fff !important;
			color: #333 !important;
			line-height: 1.4;
		}
	}

	/* 修复Android WebView中的输入框问题 */
	@supports not (-webkit-touch-callout: none) {
		input, textarea {
			background-color: #fff !important;
			color: #333 !important;
		}
	}

	/* 确保输入框不被其他元素遮挡 */
	.input-group, .form-item {
		position: relative;
		z-index: 1;
	}

	/* 修复输入框在flex布局中的问题 */
	.flex input, .flex textarea {
		flex: 1;
		background-color: #fff !important;
		color: #333 !important;
	}

	/* 全局字体设置 */
	page {
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
		background-color: #f5f5f5;
	}

	/*每个页面公共css */
	page {
		background-color: #f5f5f5;
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
	}
	
	/* 通用样式 */
	.container {
		min-height: 100vh;
	}
	
	/* 按钮样式 */
	.btn {
		padding: 20rpx 40rpx;
		border-radius: 10rpx;
		font-size: 28rpx;
		text-align: center;
		border: none;
	}
	
	.btn-primary {
		background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
		color: white;
	}
	
	.btn-secondary {
		background: #f0f0f0;
		color: #666;
	}
	
	/* 卡片样式 */
	.card {
		background: white;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.1);
	}
	
	/* 文本样式 */
	.text-primary {
		color: #667eea;
	}
	
	.text-success {
		color: #4CAF50;
	}
	
	.text-danger {
		color: #F44336;
	}
	
	.text-muted {
		color: #999;
	}
	
	/* 布局样式 */
	.flex {
		display: flex;
	}
	
	.flex-center {
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.flex-between {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	
	.flex-column {
		display: flex;
		flex-direction: column;
	}
</style>
